package com.copro.chemdb.stat.rule;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.ihelpuoo.util.Properties;

import com.copro.chemdb.result.Result;
import com.copro.chemdb.stat.AbstractStatRule;
import com.copro.chemdb.stat.StatResult;
import com.copro.chemdb.stat.StatRule;
import com.copro.chemdb.subject.Subject;
import com.copro.chemdb.subject.SubjectUtils;
import com.copro.simple.Application;
import com.copro.simple.SimpleException;

public class AccumulateStatRule extends AbstractStatRule {

	@Override
	public StatResult doStat(Application app, Subject subject, int statType)
			throws SimpleException {

		List<Subject> children = null;
		if (Result.FROM_DB == statType)
			children = SubjectUtils.queryChildrenDBSubjects(app, subject);
		else
			children = SubjectUtils.queryChildrenQuesSubjects(app, subject);
		Iterator<Subject> it = children.iterator();
		List<StatResult> reList = new LinkedList<StatResult>();
		while (it.hasNext()) {
			Subject child = it.next();
			if (child.isHasOptions())
				child.setOptions(SubjectUtils.queryOptions(app, child));
			StatRule rule = app.getStatRule(child.getRule());
			if (null == rule)
				continue;
			StatResult re = rule.doStat(app, child, statType);
			reList.add(re);
		}
		return new AccumulateStatResult("accumulate", reList);
	}

	@Override
	public void setup(Properties conf) {

	}

}
